part of common_lib_plugin;

Logger _logger = Logger(
  printer: PrettyPrinter(
    methodCount: 0,
    errorMethodCount: 8,
    colors: Platform.isAndroid,
    printEmojis: true,
    printTime: false,
  ),
);

logV(dynamic msg, {String tag = '', bool addPlugin = true}) {
  _logger.t('$tag $msg');
  if (addPlugin) {
    logPluginCtrl.addLog(LogClass(msg, Colors.grey));
  }
}

logD(dynamic msg, {String tag = '', bool addPlugin = true}) {
  _logger.d('$tag $msg');
  if (addPlugin) {
    logPluginCtrl.addLog(LogClass(msg, Colors.white));
  }
}

logI(dynamic msg, {String tag = '', bool addPlugin = true}) {
  _logger.i('$tag $msg');
  if (addPlugin) {
    logPluginCtrl.addLog(LogClass(msg, Colors.blue));
  }
}

logW(dynamic msg, {String tag = '', bool addPlugin = true}) {
  _logger.w('$tag $msg');
  if (addPlugin) {
    logPluginCtrl.addLog(LogClass(msg, Colors.orange));
  }
}

logE(dynamic msg, {String tag = '', StackTrace? stackTrace}) {
  _logger.e('$tag $msg');
  logPluginCtrl.addLog(LogClass(msg, Colors.red));
  stackLog(stackTrace: stackTrace);
}

logWTF(dynamic msg, {String tag = '', bool addPlugin = false}) {
  log('$tag $msg');
  if (addPlugin) {
    logPluginCtrl.addLog(LogClass(tag + msg, Colors.deepPurpleAccent));
  }
}

stackLog({StackTrace? stackTrace}) {
  final chain = Chain.forTrace(stackTrace ?? StackTrace.current);
  final frames = chain.toTrace().frames;
  int hideCount = 0;
  for (var frame in frames) {
    if (frame.location.startsWith('package:flutter') || frame.location.startsWith('dart:')) {
      hideCount++;
      continue;
    }
    logV('所在文件：${frame.location},line:${frame.line},column:${frame.column}');
  }
  logI('隐藏了一共$hideCount个调用栈的输出');
}

class _LogPluginCtrl extends LogCtrlable {}

var logPluginCtrl = _LogPluginCtrl();
